﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using OrderAggregator.InputFormats.Art;
using OrderAggregator.InputFormats.Excel;
using OrderAggregator.InputFormats.NewMag;
using OrderAggregator.InputFormats.MosMart;
using OrderAggregator.InputFormats.GlobalMarket;
using OrderAggregator.InputFormats.HyperNovosib;
using OrderAggregator.InputFormats.HyperOmsk;
using OrderAggregator.InputFormats.Novoaltaisk;
using OrderAggregator.InputFormats.Omsk;
using OrderAggregator.InputFormats.KapitalAlfa;


namespace OrderAggregator.Tests
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            Test1();
            Test2();

            
            Application.Run(new Form1());
        }

        /// <summary>
        /// Тест на парсинг по отдельности каждого формата
        /// </summary>
        private static void Test1()
        {
            ExcelTest();
            HolidayTest();
            AshanTest();
            NewMagTest();
            ArtTest();
            MosMartTest();
            GlobalMarketTest();
            HyperNovosibTest();
            HyperOmskTest();


            OmskTest();
            Excel2Test();
            KapitalAlfaTest();

            ExcelNewFormatTest();


            //NovoaltaiskTest();
        }

        /// <summary>
        /// Тест на парсинг всей системы (тест пересечения форматов)
        /// </summary>
        private static void Test2()
        {
            var testDataDir = System.Reflection.Assembly.GetExecutingAssembly().Location;
            testDataDir = Path.GetDirectoryName(testDataDir);
            testDataDir = Path.Combine(testDataDir, "TestData");

            var subDirs = Directory.GetDirectories(testDataDir);

            foreach (var subDir in subDirs)
            {
                var inputFiles = Directory.GetFiles(subDir);

                foreach (var inputFile in inputFiles)
                {
                    //не обрабатываем пдф-ы, 
                    if (Path.GetExtension(inputFile) == ".pdf")
                    {
                        continue;
                    }

                    var order =
                    OrderAggregator.Core.Aggregator.ParseInput(
                    inputFile,
                    OrderAggregator.InputFormats.BaseOrderFormat.AllFormats
                    );

                    Assert(order != null);
                }
            }

            

            
        }

        private static void ExcelTest()
        {
            var excelFormat = ExcelFormat.Instance;

            var order = excelFormat.ParseAndTranslate("TestData\\Excel\\Z1.xls");
        }

        private static void MAriaRaTest()
        {
            var format = OrderAggregator.InputFormats.MariaRA.MariaRaFormat.Instance;

            //format.ParseAndTranslate()
        }

        private static void HolidayTest()
        {
            var format = OrderAggregator.InputFormats.Holiday.HolidayCsvFormat.Instance;

            string filePath = "TestData\\Holiday\\doc_45088847_120815_133507_98211277.csv";

            var order = format.ParseAndTranslate(filePath);

			Assert(order != null);
        }

        private static void AshanTest()
        {
            var format = OrderAggregator.InputFormats.Ashan.AshanFormat.Instance;

            string filePath = "TestData\\Ashan\\New\\hcdeedeo9061-195.txt";
            var order = format.ParseAndTranslate(filePath);

			Assert(order != null);

            filePath = "TestData\\Ashan\\hcdeedeo6.txt";
            order = format.ParseAndTranslate(filePath);

			Assert(order != null);

            filePath = "TestData\\Ashan\\two_pages.txt";
            order = format.ParseAndTranslate(filePath);

			Assert(order != null);
        }

		private static void NewMagTest()
		{
            var excelFormat = NewMagFormat.Instance;

			var order = excelFormat.ParseAndTranslate("TestData\\NewMag\\ЗАЯВКА № 40 НА 2610.xls");
			Assert(order.OrderRows.Count == 25);

			order = excelFormat.ParseAndTranslate("TestData\\NewMag\\ЗАЯВКА № 111 НА 3110.xls");
			Assert(order.OrderRows.Count == 20);

			order = excelFormat.ParseAndTranslate("TestData\\NewMag\\ЗАЯВКА № 75 НА 0111.xls");
			Assert(order.OrderRows.Count == 47);
		}


		private static void ArtTest()
		{
			var excelFormat = ArtFormat.Instance;

			var order = excelFormat.ParseAndTranslate("TestData\\Art\\Заказ Компания АРТ (3277).xls");
			Assert(order.OrderRows.Count == 9);

			order = excelFormat.ParseAndTranslate("TestData\\Art\\Заказ Компания АРТ (3278).xls");
			Assert(order.OrderRows.Count == 61);

			order = excelFormat.ParseAndTranslate("TestData\\Art\\Заказ Компания АРТ (3329).xls");
			Assert(order.OrderRows.Count == 10);

			order = excelFormat.ParseAndTranslate("TestData\\Art\\Заказ Компания АРТ (3336).xls");
			Assert(order.OrderRows.Count == 62);

		}

        private static void MosMartTest()
        {
            var mosMartFormat = MosMartFormat.Instance;

            var order = mosMartFormat.ParseAndTranslate("TestData\\MosMart\\193455.xlsx");
            Assert(order.OrderRows.Count == 9);

            order = mosMartFormat.ParseAndTranslate("TestData\\MosMart\\220991 шустова.xls");
            Assert(order.OrderRows.Count == 25);

            order = mosMartFormat.ParseAndTranslate("TestData\\MosMart\\221319 на 5.04.2013 Шустова.xlsx");
            Assert(order.OrderRows.Count == 14);

            order = mosMartFormat.ParseAndTranslate("TestData\\MosMart\\223076.xls");
            Assert(order.OrderRows.Count == 7);

            order = mosMartFormat.ParseAndTranslate("TestData\\MosMart\\223922.xlsx");
            Assert(order.OrderRows.Count == 13);

            order = mosMartFormat.ParseAndTranslate("TestData\\MosMart\\456432.xlsx");
            Assert(order.OrderRows.Count == 10);

            order = mosMartFormat.ParseAndTranslate("TestData\\MosMart\\шустова392497.xlsx");
            Assert(order.OrderRows.Count == 24);           
        }

        private static void GlobalMarketTest()
        {
            var globalMarketFormat = GlobalMarketFormat.Instance;

            var order = globalMarketFormat.ParseAndTranslate("TestData\\GlobalMarket\\БЕРИНГА.xls");
            Assert(order.OrderRows.Count == 18);

            order = globalMarketFormat.ParseAndTranslate("TestData\\GlobalMarket\\КУРЧАТОВА.xls");
            Assert(order.OrderRows.Count == 15);

            order = globalMarketFormat.ParseAndTranslate("TestData\\GlobalMarket\\МИРА.xls");
            Assert(order.OrderRows.Count == 13);

            order = globalMarketFormat.ParseAndTranslate("TestData\\GlobalMarket\\учебная.xls");
            Assert(order.OrderRows.Count == 12);

            order = globalMarketFormat.ParseAndTranslate("TestData\\GlobalMarket\\Заявка на закуп ~ПрП-036903.xls");
            Assert(order.OrderRows.Count == 8);
        }

        private static void HyperNovosibTest()
        {
            var hyperNovosibFormat = HyperNovosibFormat.Instance;

            var order = hyperNovosibFormat.ParseAndTranslate("TestData\\HyperNovosib\\PurchOrder_0038429F100748BF97B3AF9E9D10D916.htm");
            Assert(order.OrderRows.Count == 30);

            order = hyperNovosibFormat.ParseAndTranslate("TestData\\HyperNovosib\\PurchOrder_A1796679C0A34C88B017395621491FAA.htm");
            Assert(order.OrderRows.Count == 33);

            order = hyperNovosibFormat.ParseAndTranslate("TestData\\HyperNovosib\\PurchOrder_FB374F3152DA4B01906B478F1A11715C.htm");
            Assert(order.OrderRows.Count == 1);
        }

        private static void HyperOmskTest()
        {
            var hyperOmskFormat = HyperOmskFormat.Instance;

            var order = hyperOmskFormat.ParseAndTranslate("TestData\\HyperOmsk\\шустова готовая.rtf");
            Assert(order.OrderRows.Count == 32);

            order = hyperOmskFormat.ParseAndTranslate("TestData\\HyperOmsk\\шустова охл.rtf");
            Assert(order.OrderRows.Count == 6);

            order = hyperOmskFormat.ParseAndTranslate("TestData\\HyperOmsk\\шустова.rtf");
            Assert(order.OrderRows.Count == 37);    
        }

        private static void NovoaltaiskTest()
        {
            var NovoaltaiskFormat = new NovoaltaiskFormat();

            var order = NovoaltaiskFormat.ParseAndTranslate("TestData\\Novoaltaisk\\osui337_108673.pdf");
            Assert(order.OrderRows.Count == 22);

            order = NovoaltaiskFormat.ParseAndTranslate("TestData\\Novoaltaisk\\osui526_109753.pdf");
            Assert(order.OrderRows.Count == 6);
        }


        private static void OmskTest()
        {
            var omskFormat = OmskFormat.Instance;

            var order = omskFormat.ParseAndTranslate("TestData\\Omsk\\Result-406386947.xls");
            Assert(order.OrderRows.Count == 18);

            order = omskFormat.ParseAndTranslate("TestData\\Omsk\\Result-406412035.xls");
            Assert(order.OrderRows.Count == 14);
        }

        private static void Excel2Test()
        {
            var excel2Format = Excel2Format.Instance;

            var order = excel2Format.ParseAndTranslate("TestData\\Excel\\11768013.xls");
            Assert(order.OrderRows.Count == 8);

            order = excel2Format.ParseAndTranslate("TestData\\Excel\\11768193.xls");
            Assert(order.OrderRows.Count == 25);
        }

        private static void KapitalAlfaTest()
        {
            var kapitalAlfaFormat = KapitalAlfaFormat.Instance;

            var order = kapitalAlfaFormat.ParseAndTranslate("TestData\\KapitalAlfa\\Шустова.xls");
            Assert(order.OrderRows.Count == 8);

            order = kapitalAlfaFormat.ParseAndTranslate("TestData\\KapitalAlfa\\Шустова1.xls");
            Assert(order.OrderRows.Count == 17);

            order = kapitalAlfaFormat.ParseAndTranslate("TestData\\KapitalAlfa\\Шустова2.xls");
            Assert(order.OrderRows.Count == 25);
        }

        private static void ExcelNewFormatTest()
        {
            var format = ExcelNewFormat.Instance;

            var order = format.ParseAndTranslate("TestData\\ExcelNew\\20130710_Шустова_Светлана___енадьевна_ИП_13926.xls");
            Assert(order.OrderRows.Count == 9);
            
            order = format.ParseAndTranslate("TestData\\ExcelNew\\20130710_Шустова_Светлана___енадьевна_ИП_91196.xls");
            Assert(order.OrderRows.Count == 6);

            order = format.ParseAndTranslate("TestData\\ExcelNew\\заявка Шустова 11.012.13.xls");
            Assert(order.OrderRows.Count == 26);

            order = format.ParseAndTranslate("TestData\\ExcelNew\\Шустова.xlsx");
            Assert(order.OrderRows.Count == 34);

            order = format.ParseAndTranslate("TestData\\ExcelNew\\Шустова 2.xls");
            Assert(order.OrderRows.Count == 30);

        }
        


    	private static void Assert(bool trueExpression)
		{
			if (!trueExpression)
			{
				throw new Exception();
			}
		}
    }
}
